// 根据id获取分类列表数据
import { onBeforeRouteUpdate } from "vue-router";
import { getCategoryApi } from "@/apis/cateGory";
import { useRoute } from "vue-router";
import { onMounted, ref } from "vue";
export function useCategory() {
  const categoryData = ref({});
  const route = useRoute();
  const getCategory = async (id = route.params.id) => {
    // 如何在setup中获取路由参数 useRoute() -> route 等价于this.$route
    const res = await getCategoryApi(id);
    categoryData.value = res.result;
  };
  // 解决路由缓存问题
  onBeforeRouteUpdate((to) => {
    getCategory(to.params.id);
  });
  onMounted(() => getCategory(route.params.id));
  return {
    categoryData,
  };
}
